class Pet {
  final int id;
  final String name;
  final String species; // 种类：猫、狗等
  final String breed;   // 品种
  final int age;
  final String gender;
  final String description;
  final List<String> images;
  final int ownerId;
  final bool isForAdoption;
  final DateTime createdAt;

  Pet({
    required this.id,
    required this.name,
    required this.species,
    required this.breed,
    required this.age,
    required this.gender,
    required this.description,
    required this.images,
    required this.ownerId,
    required this.isForAdoption,
    required this.createdAt,
  });

  // 从JSONPlaceholder的/photos数据转换成Pet对象
  factory Pet.fromJson(Map<String, dynamic> json) {
    return Pet(
      id: json['id'],
      name: json['title']?.split(' ')[0] ?? 'Unknown',
      species: _getRandomSpecies(),
      breed: _getRandomBreed(json['id'] % 2 == 0 ? 'cat' : 'dog'),
      age: (json['id'] % 10) + 1,
      gender: json['id'] % 2 == 0 ? '公' : '母',
      description: json['title'] ?? '',
      images: [json['url'], json['thumbnailUrl']].where((url) => url != null).cast<String>().toList(),
      ownerId: json['albumId'] ?? 1,
      isForAdoption: json['id'] % 3 == 0,
      createdAt: DateTime.now().subtract(Duration(days: json['id'] % 30)),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'species': species,
      'breed': breed,
      'age': age,
      'gender': gender,
      'description': description,
      'images': images,
      'ownerId': ownerId,
      'isForAdoption': isForAdoption,
      'createdAt': createdAt.toIso8601String(),
    };
  }

  // 辅助方法 - 随机生成宠物种类
  static String _getRandomSpecies() {
    List<String> species = ['猫', '狗', '兔子', '仓鼠', '鸟'];
    return species[DateTime.now().millisecond % species.length];
  }

  // 辅助方法 - 根据种类随机生成品种
  static String _getRandomBreed(String species) {
    Map<String, List<String>> breeds = {
      'cat': ['波斯猫', '布偶猫', '暹罗猫', '英国短毛猫', '苏格兰折耳猫'],
      'dog': ['金毛', '拉布拉多', '哈士奇', '柯基', '边牧', '泰迪'],
      'rabbit': ['荷兰侏儒兔', '垂耳兔', '安哥拉兔'],
      'hamster': ['仓鼠', '布丁仓鼠', '三线仓鼠'],
      'bird': ['虎皮鹦鹉', '鸡尾鹦鹉', '金丝雀'],
    };
    
    List<String> typeBreeds = breeds[species] ?? ['未知品种'];
    return typeBreeds[DateTime.now().millisecond % typeBreeds.length];
  }
} 